#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

//查找总价格为目标值的两个物品
class Solution {
public:
    vector<int> twoSum(vector<int>& price, int target)
    {
        int left = 0;
        int right = price.size() - 1;
        while (left < right)
        {
            if (price[left] + price[right] > target)
            {
                right--;
            }
            else if (price[left] + price[right] < target)
            {
                left++;
            }
            else {
                vector<int> ret = { price[left],price[right] };
                return ret;
            }
        }
        vector<int> ans = { 0 };
        return ans;
    }
};

//三数之和
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>> ret;
        for (int i = 0; i < n; )
        {
            if (nums[i] > 0)
            {
                break;
            }
            int left = i + 1;
            int right = n - 1;
            int target = -nums[i];
            while (left < right)
            {
                if (nums[left] + nums[right] > target)
                {
                    right--;
                }
                else if (nums[left] + nums[right] < target)
                {
                    left++;
                }
                else {
                    ret.push_back({ nums[i],nums[left],nums[right] });
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1]) left++;
                    while (left < right && nums[right] == nums[right + 1]) right--;
                }
            }
            i++;
            while (i < n && nums[i] == nums[i - 1]) i++;

        }

        return ret;
    }
};

//四数之和

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target)
    {
        sort(nums.begin(), nums.end());
        vector<vector<int>> ret;
        int n = nums.size();
        for (int i = 0; i < n; )
        {
            for (int j = i + 1; j < n; )
            {
                int left = j + 1;
                int right = n - 1;
                while (left < right)
                {
                    long long sum = (long long)target - nums[i] - nums[j];
                    if (nums[left] + nums[right] > sum)
                    {
                        right--;
                    }
                    else if (nums[left] + nums[right] < sum)
                    {
                        left++;
                    }
                    else
                    {
                        ret.push_back({ nums[i],nums[j],nums[left],nums[right] });
                        left++;
                        right--;
                        while (left < right && nums[left] == nums[left - 1]) left++;
                        while (left < right && nums[right] == nums[right + 1]) right--;
                    }

                }
                j++;
                while (j < n && nums[j] == nums[j - 1]) j++;

            }
            i++;
            while (i < n && nums[i] == nums[i - 1]) i++;

        }
        return ret;
    }
};
int main()
{

    return 0;
}